package com.example.hutool;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.RandomUtil;

import java.awt.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * RandomUtil
 *
 * @author 江黎
 * @since 2022-11-05
 */
public class 随机数工具类 {

    public static void main(String[] args) {
        // 1.随机获得列表中的元素
        List<String> list = CollUtil.newArrayList("east", "south", "west", "north");
        System.out.println(RandomUtil.randomEle(list));
        // 2.权重随机算法实现
        // 有时候我们希望随机的值有固定的概率
        WeightRandom<String> weightRandom = new WeightRandom<>();
        weightRandom.add(new WeightRandom.WeightObj<>("AAA", 1));
        weightRandom.add(new WeightRandom.WeightObj<>("BBB", 10));
        weightRandom.add(new WeightRandom.WeightObj<>("CCC", 39));
        weightRandom.add(new WeightRandom.WeightObj<>("DDD", 60));
        for (int i = 0; i < 10; i++) {
            String next = weightRandom.next();
            System.out.println(next);
        }

        // 3.随机日期
        for (int i = 0; i < 10; i++) {
            DateTime dateTime = RandomUtil.randomDay(-365, 365);
            System.out.println(dateTime);
        }

        // 4.随机时间
        Date date = new Date();
        int min = -365 * 86400;
        int max = 365 * 86400;
        for (int i = 0; i < 10; i++) {
            DateTime dateTime = RandomUtil.randomDate(date, DateField.SECOND, min, max);
            System.out.println(dateTime);
        }

        // 5.随机颜色
        Color color = ImgUtil.randomColor();
        System.out.println(color);
        System.out.println(color.getRed());
        System.out.println(color.getBlue());
        System.out.println(color.getRGB());

        // 6.随机索引
        int[] randomInts = RandomUtil.randomInts(10);
        System.out.println(Arrays.toString(randomInts));

        // 7.指定范围随机字符
        System.out.println(RandomUtil.randomChar("ABCDEFG"));

        // 8.随机汉字
        System.out.println(RandomUtil.randomChinese());

        // 9.随机指定范围内的整数
        System.out.println(RandomUtil.randomInt(10, 100));

        // 10.指定长度的数字字符串
        System.out.println(RandomUtil.randomNumbers(6));

        // 11.随机指定长度的字符串
        System.out.println(RandomUtil.randomString(6));

    }

}
